package org.particles.domain.world;

import org.particles.domain.Particle;
import org.particles.domain.Vector;
import org.particles.domain.Vector2D;

/**
 * slow particles.
 */
public class CoolingOutOfBoundResolver implements OutOfWorldBoundResolver {
	private double slowFactor;
	private boolean considerBounds;
	
	public CoolingOutOfBoundResolver(final double slowFactor) {
		this.slowFactor = slowFactor;
	}

	/**
	 * @param considerBounds if this resolver should slow particles only
	 * if particle is out of bound (true) or always (false).
	 */
	public CoolingOutOfBoundResolver(final double slowFactor, final boolean considerBounds) {
		this.slowFactor = slowFactor;
		this.considerBounds = considerBounds;
	}

	@Override
	public void resolve(World world, int particleIndex) {
		final Particle particle = world.getParticles().get(particleIndex);
		final Vector2D place = (Vector2D) particle.getSpacePlace();
		final Vector2D worldDim = (Vector2D) world.getDimension();
		final double min = 0;
		final Vector impulse = particle.getImpulse();
		if(!considerBounds) {
			impulse.setLength(impulse.length() / this.slowFactor);
		}
		else if(place.getX() < min || place.getX() > worldDim.getX()
				|| place.getY() < min || place.getY() > worldDim.getX()) {
			impulse.setLength(impulse.length() / this.slowFactor);
		}
	}

	public void setSlowFactor(double slowFactor) {
		this.slowFactor = slowFactor;
	}

	public double getSlowFactor() {
		return slowFactor;
	}

}
